using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine;

[Serializable]
public class FlyToStackComponent : BaseComponent
{
    [Title("世界坐标下的间隔值，旋转后需要独自运算")]
    public float StackOffsetX;
    public float StackOffsetY; //堆叠间隔
    public float StackOffsetZ; //堆叠间隔
    public float StackDirection = 0.3f;
    private Vector3 InitScale;

    public override void OnAwake()
    {
        base.OnAwake();
        InitScale = entity.transform.localScale;
    }
    public void FlyToStack(Entity StackTransform, Action<Entity> actions = null)
    {
        if (StackTransform == null || !StackTransform.HasEcsComponent<StackComponent>())
        {
            Debug.LogError("试图在一个不符合规则的物体上堆叠" + StackTransform.name);
            return;
        }
        StackComponent stackComponent = StackTransform.GetEcsComponent<StackComponent>();
        int index = stackComponent.StackObjectLists.Count;
        Vector3 targetPosition = stackComponent.GetStackObjectPosition(index, StackOffsetX, StackOffsetY, StackOffsetZ);

        entity.transform.DOKill();

        entity.transform.DOJump(targetPosition, 1, 0, StackDirection).SetEase(Ease.InQuad).OnUpdate(() =>
        {
            targetPosition = stackComponent.GetStackObjectPosition(index, StackOffsetX, StackOffsetY, StackOffsetZ);
        }).OnComplete(() =>
        {
            StackTransform.GetEcsComponent<StackComponent>().AddStack(entity);
            entity.transform.localScale = Vector3.zero;
            entity.transform.SetParent(stackComponent.entity.transform);
            entity.transform.localEulerAngles = stackComponent.backRot;
            // 设置木头的父级为Player   
            entity.transform.DOScale(InitScale, 0.3f).SetEase(Ease.OutBack);
            actions?.Invoke(entity);
        });
    }
}
